<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>相册列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

<!--    <el-empty description="当前页面正在开发中，敬请期待……"></el-empty>-->
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="编号"
          width="50"
      align="center">
      </el-table-column>
      <el-table-column
          prop="name"
          label="姓名" align="center">
      </el-table-column>
      <el-table-column
          prop="description"
          label="简介"  header-align="center">
      </el-table-column>
      <el-table-column
          prop="sort"
          label="自定义排序"  align="center">
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="openDelectConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹出的编辑相册的对话框 -->
    <el-dialog title="修改相册"  :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="相册名称":label-width="formLabelWidth">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="简介" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.description"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.sort"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      ruleForm: {
        id:'',
        name: '',
        description: ''
      },
      formLabelWidth: '120px'
    }
  },
  methods:{
    submitEdit(){
      let url='http://localhost:9080/albums/'+this.ruleForm.id+'/update';
      let formData = this.qs.stringify(this.ruleForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改相册成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadAlbumList();
        } else if (responseBody.state == 40900) {
          this.$message.error(responseBody.message);
        } else {
          this.$message.error(responseBody.message);
          this.dialogFormVisible = false;
          this.loadAlbumList();
        }
      });
    },
    handleEdit(album) {
      let message = '您正在尝试编辑【' + album.id + '-' + album.name + '】的相册详情，抱歉，该功能尚未实现……';
        // this.ruleForm=album;
      let url='http://localhost:9080/albums/'+album.id
      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
            let responseBody=response.data;
        if(responseBody.state==20000){
          this.ruleForm=responseBody.data;
          this.dialogFormVisible = true;
        }else {
          this.$message.error(responseBody.message)
          this.loadAlbumList()
        }
      })
    },
    handleDelect(album){
      console.log("准备删除"+album.id)
      let url='http://localhost:9080/albums/'+album.id+'/delete';
      this.axios.post(url).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.loadAlbumList();
          this.$message({
            type: 'success',
            message: '删除成功'
          });
        }else {
          this.$message({
            type: 'info',
            message: responseBody.message
          });
          this.loadAlbumList();
        }
      })
    },
    openDelectConfirm(album){
      let message='此操作将永久删除该文件['+album.name+'], 是否继续?';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelect(album);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    loadAlbumList(){
      console.log('loadAlbumList方法')
      let url='http://localhost:9080/albums';
      console.log('url='+url);
      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let responseBody=response.data;
        this.tableData=responseBody.data;
      })
    }
  },
  mounted() {//挂载
    console.log('mounted')
    this.loadAlbumList();
  }

}
</script>